package com.wealth.system.web.controller;

import java.util.Date;

import iaas.enums.StatusEnum;
import iaas.redis.RedisService;
import iaas.utils.ResultObject;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.wealth.member.entity.Member;
import com.wealth.member.entity.Membership;
import com.wealth.member.service.IMemberService;
import com.wealth.member.service.IMembershipService;
import com.wealth.order.service.IOrderService;
import com.wealth.system.entity.Corporation;

/**
 * 注册
 * @author zjw
 *
 */
@Controller
@RequestMapping(value="/reg")
public class RegController extends BaseController {

	Logger logger = Logger.getLogger(RegController.class);
 
	@Autowired
	private RedisService redisService;
	
	@Autowired
	private IMemberService memberService;
	
	@Autowired
	private IMembershipService membershipService; 
	
	@Autowired
	private IOrderService orderService;
	
	/**
	 * 注册页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String reg(){
		return "reg";
	}
	
	
	/**
	 * 注册
	 */
	@RequestMapping(method=RequestMethod.POST)
	@ResponseBody
	public ResultObject doReg(Member member,String random,RedirectAttributes redirectAttributes,HttpServletRequest request){
	
	try {
		
		Member m = memberService.findMemberByUsername(member.getUsername());
		//用户名和密码不为空表示已经注册
		if(m!=null && m.getUsername()!=null && m.getPassword()!=null){ 
			return new ResultObject("500", "该手机号码已经注册", null);
		} 
		
		String randomInRedis = redisService.get("redis_sms_"+member.getUsername());
	    System.out.println(randomInRedis);
		if(!randomInRedis.equals(random)){
			return new ResultObject("500", "验证码错误", null);
		}
		 
		//微信端注册，并且绑定手机号码,但密码为空,只更新密码
		if(m!=null && m.getUsername()!=null && m.getPassword() == null){
			m.setPassword(member.getPassword()); 
			memberService.save(m);
			
		}
		//没有此号码的会员数据
		if(m==null){ 
			member.setStatus(StatusEnum.NORMAL.getValue());
			member.setCreateTime(new Date());
			memberService.save(member); 
			Membership membership = new Membership();
			membership.setCorp(new Corporation(corpid));
			membership.setMember(member);
			membershipService.save(membership);
		}
		
		/** -------- 会员登陆-------- **/
		//FIXME 密码明文处理
		UsernamePasswordToken token = new UsernamePasswordToken();  
	    token.setUsername(member.getUsername());  
	    token.setPassword(member.getPassword().toCharArray());  
	    token.setRememberMe(true);
	    SecurityUtils.getSubject().login(token);  
	    
	    return new ResultObject("200", "注册成功", null);
	   } catch (Exception e) {
		    logger.error("注册出错！", e);
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	//订单列表
		@RequestMapping(value="/status/{status}")
		public String listForm(@PathVariable Integer status,Model model){
			try {
				int count = orderService.countOrder(memberid, status);
				model.addAttribute("count", count);
				model.addAttribute("status", status);
				return "order/list";
			} catch (Exception e) {
				logger.error("订单列表错误", e);
				return "system/500";
			}
		}
}
